package com.lfg.leetcode.leetcode1500;


import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;


public class No1765highestPeak {
    public static void main(String[] args) {
        int[][] properties = {{0, 0, 1}, {1, 0, 0}, {0, 0, 0}};
        System.out.println(JSON.toJSONString(highestPeak(properties)));
    }


    public static int[][] highestPeak(int[][] isWater) {
        Queue<int[]> queue = new LinkedList<>();
        int row = isWater.length - 1;
        int column = isWater[0].length - 1;
        int[][] ret = new int[isWater.length][isWater[0].length];
        for (int[] ints : ret) {
            Arrays.fill(ints, 0);
        }

        for (int i = 0; i < isWater.length; i++) {
            for (int j = 0; j < isWater[i].length; j++) {
                if (isWater[i][j] == 1) {
                    queue.add(new int[]{i, j});
                }
            }
        }
        while (!queue.isEmpty()) {
            int[] matrix = queue.poll();
            int r = matrix[0];
            int c = matrix[1];
            if (r > 0) {
                int r2 = r - 1;
                if (isWater[r2][c] == 0) {
                    isWater[r2][c] = 2;
                    ret[r2][c] = ret[r][c] + 1;
                    queue.add(new int[]{r2, c});
                }
            }
            if (r < row) {
                int r2 = r + 1;
                if (isWater[r2][c] == 0) {
                    isWater[r2][c] = 2;
                    ret[r2][c] = ret[r][c] + 1;
                    queue.add(new int[]{r2, c});
                }
            }
            if (c > 0) {
                int c2 = c - 1;
                if (isWater[r][c2] == 0) {
                    isWater[r][c2] = 2;
                    ret[r][c2] = ret[r][c] + 1;
                    queue.add(new int[]{r, c2});
                }
            }
            if (c < column) {
                int c2 = c + 1;
                if (isWater[r][c2] == 0) {
                    isWater[r][c2] = 2;
                    ret[r][c2] = ret[r][c] + 1;
                    queue.add(new int[]{r, c2});
                }
            }
        }
        return ret;
    }

}

